package wsepForum.ui.swing.innerPanels;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.concurrent.ExecutionException;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import wsepForum.common.elements.Section;
import wsepForum.domain.exceptions.UnAuthorizedException;
import wsepForum.network.common.GUIAnswer;
import wsepForum.network.common.GUIAnswer.AnswerType;
import wsepForum.network.common.GUIRequest.RequestType;
import wsepForum.persistance.exceptions.WrongElementIDException;
import wsepForum.persistance.exceptions.WrongUserNameException;
import wsepForum.ui.swing.outerPanels.AOuterPanel;
import wsepForum.ui.swing.workers.ForumWorker;

public class EditSectionDetailsPanel extends AManageSectionPanel {

	private static final long serialVersionUID = -2865916532278275462L;

	private Section sectionCurrState;

	private JList currModList;
	
	public EditSectionDetailsPanel(AOuterPanel panel, Section currState, Vector<String> userList) {
		super(panel, userList);
		this.sectionCurrState = currState;
		initPanel();
	}

	protected void initPanel(){
		JLabel lblSelectModeratorsFor = new JLabel("remove moderators:");
		lblSelectModeratorsFor.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblSelectModeratorsFor.setBounds(400, 100, 210, 14);
		add(lblSelectModeratorsFor);
		
		JScrollPane currModScroll = new JScrollPane();
		currModScroll.setBounds(430, 125, 116, 189);
		add(currModScroll);
			
		this.currModList = new JList(this.sectionCurrState.getAllSectionMods());
		currModScroll.setViewportView(currModList);
				
		super.initPanel();
	}
	
	@Override
	public void addSubmitFunctionality() {
		this.btnSubmit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(outerPanel.getConnection()){

					@Override
					protected AInnerPanel doInBackground() throws Exception {
						AInnerPanel returnedPanel = EditSectionDetailsPanel.this;
						
						if(!allUsersList.isSelectionEmpty() && !currModList.isSelectionEmpty()){
							returnedPanel = new SectionPanel(sectionCurrState,true,outerPanel);
							GUIAnswer submitedAns = null;
	
							for (int selected : allUsersList.getSelectedIndices()) {
								submitedAns = request(RequestType.ADD_MOD_TO_SECTION, new Object[] {sectionCurrState.getElementID(),usernameList.get(selected)});
								if(submitedAns.getAnswerType() == GUIAnswer.AnswerType.EXCEPTION) 
									handleSubmitExceptions((Exception) submitedAns.getArguments()[0]);
							}
							
							for (int selected : currModList.getSelectedIndices()) {
								submitedAns = request(RequestType.REMOVE_MOD, new Object[] {sectionCurrState.getElementID(),sectionCurrState.getAllSectionMods().get(selected)});
								if(submitedAns.getAnswerType() == GUIAnswer.AnswerType.EXCEPTION) 
									handleSubmitExceptions((Exception) submitedAns.getArguments()[0]);
							}
							outerPanel.setNumOfConnected(submitedAns.getNumOfConnected());
							if(submitedAns.getAnswerType().equals(AnswerType.ANSWER)){
								returnedPanel = new SectionPanel((Section) submitedAns.getArguments()[0],true,outerPanel);
							}
							else {
								returnedPanel = handleSubmitExceptions((Exception) submitedAns.getArguments()[0]);
							}
						}
						else {
							JOptionPane.showMessageDialog(null, "you must select at least one user to add\remove");
						}
						return returnedPanel;
					}

					@Override
					public void done(){
						try {
							outerPanel.swapInnerPanel(get());
						} catch (InterruptedException e) {
							outerPanel.swapInnerPanel(handleSubmitExceptions(e));
						} catch (ExecutionException e) {
							outerPanel.swapInnerPanel(handleSubmitExceptions(e));
						}
					}
					
					private AInnerPanel handleSubmitExceptions(Exception thrown){
						try {
							throw thrown;
						} catch (WrongElementIDException e) {
							JOptionPane.showMessageDialog(null, "the section you are trying to edit was deleted by another user", "Section deleted",JOptionPane.ERROR_MESSAGE);
							return requestHomePanel(outerPanel);
						} catch (WrongUserNameException e) {
							JOptionPane.showMessageDialog(null, "the user " + e.getWrongUsername() + " was deleted from the system,\nand therefore cannot be add\remove from the section ", "Section deleted",JOptionPane.ERROR_MESSAGE);
							return EditSectionDetailsPanel.this;
						} catch (UnAuthorizedException e) {
							JOptionPane.showMessageDialog(null, "you are not authorized to do that", "Unauthorized use",JOptionPane.ERROR_MESSAGE);
							return requestHomePanel(outerPanel);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(null, "operation failed! please try again\n" + e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
							return EditSectionDetailsPanel.this;
						}
					}				
				};
				worker.execute();
			}
		});		
	}

	@Override
	protected void setDitails() {
		this.sectionNameTextField.setText(this.sectionCurrState.getHeadLine());
		this.sectionNameTextField.setEnabled(false);
		this.usernameList.removeAll(this.sectionCurrState.getAllSectionMods());
		this.allUsersList.setListData(this.usernameList);
		this.currModList.setListData(this.sectionCurrState.getAllSectionMods());
	}
	
	
	
}
